package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;

import com.joplus.housekeeper.utils.L;


import org.json.JSONObject;

public class EaseChatRowText_points extends EaseChatRow {

    private TextView contentView;

    private TextView tv_result;
    String rewardId;
    private TextView tv_points;


    public EaseChatRowText_points(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_points : R.layout.ease_row_sent_message, this);
    }

    @Override
    protected void onFindViewById() {
        tv_points = (TextView) findViewById(R.id.tv_points);
        View include = findViewById(R.id.iv);
        include.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //card

/*
        ImageView iv = (ImageView) findViewById(R.id.iv);
        tv_result = (TextView) findViewById(R.id.tv_result);
        SpannableString spannableString = new SpannableString(" ");
        spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.jo_mian)), 10, 11,
                SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new RelativeSizeSpan(1.5f), 8, 10, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_result.setText(spannableString);*/

    }

    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();

        try {
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            String message = chatCard.getMessage();
          /*  int i = message.indexOf(">");
            int i1 = message.indexOf("<");
            int i2 = message.lastIndexOf(">");
            int i3 = message.lastIndexOf("<");
            String substring = message.substring(0, i1 - 1);
            String substring1 = message.substring(i, i3);
            String substring2 = message.substring(i2 + 1);

            SpannableString spannableString = new SpannableString(substring + substring1 + substring2);
            spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.jo_mian)), substring.length(), substring.length() + substring1.length(),
                    SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            spannableString.setSpan(new RelativeSizeSpan(1.5f), substring.length(), substring.length() + substring1.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            tv_points.setText(spannableString);*/
            tv_points.setText(Html.fromHtml(message));
        } catch (Exception e) {

        }
//        handleTextMessage();
        try {
            handleTextMessage();
        } catch (Exception e) {
        }


    }


    private void setTip(final ChatCard chatCard) {

//        tv_result.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(context, EvaluateActivity.class);
//                context.startActivity(intent);
//            }
//        });

    }


    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }


}
